<template>
  <div class="editable-cell">
    <a-icon type="close" class="red editable-cell-icon" @click="remove" />
    <div v-if="editable" class="editable-cell-input-wrapper">
      <a-input
        :value="value"
        autofocus
        @change="handleChange"
        @pressEnter="check"
        @blur="check"
      /><a-icon
        type="check"
        class="editable-cell-icon-check icon"
        @click="check"
      />
    </div>
    <div v-else class="editable-cell-text-wrapper">
      {{ value || " " }}
      <a-icon type="edit" class="editable-cell-icon icon" @click="edit" />
    </div>
  </div>
</template>
<script>
export default {
  props: {
    text: String,
    ifCanEdit: Boolean,
  },
  data() {
    return {
      value: this.text,
      editable: this.ifCanEdit,
    }
  },
  methods: {
    handleChange(e) {
      const value = e.target.value
      this.value = value
    },
    check() {
      this.editable = false
      this.$emit('change', this.value)
    },
    edit() {
      this.editable = true
    },
    remove() {
      this.$emit('remove', this.value)
    },
  },
}
</script>
<style lang="less" scoped>
.editable-cell {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  .editable-cell-input-wrapper {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
  .red {
    color: red;
    margin-right: 10px;
  }
  .edit {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
  }
  .icon {
    margin-left: 10px;
  }
}
</style>
